﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController : MonoBehaviour {

    public AudioSource music;

    // 玩家跟随弡相机
    public Camera PlayerCamera;
    public Player PlayerPrefeb;
    
    // Use this for initialization
    void Start () {
        Debuger.EnableLog = true;

        Datas.Singleton.Instance.playerRole = (Player)Instantiate(this.PlayerPrefeb, Utils.RandomPos(), Quaternion.identity);
        Datas.Singleton.Instance.playerRole.playerData = new PlayerData(PlayerCamera);

        InvokeRepeating("autoFire", 2, Datas.Singleton.Instance.playerRole.playerData.autoFireCd);
    }
	
	// Update is called once per frame
	void FixedUpdate () {
        Datas.Singleton.Instance.playerRole.transform.position = new Vector3(this.PlayerCamera.transform.position.x, this.PlayerCamera.transform.position.y, -0.15f);
        this.music.transform.position = Datas.Singleton.Instance.playerRole.transform.position;

        foreach(Bullet bullet in Datas.Singleton.Instance.playerRole.Bullets.transform.GetComponentsInChildren<Bullet>())
        {
            bullet.Move();
        }
	}

    void autoFire()
    {
        float degree = Datas.Singleton.Instance.playerRole.PlayerPrefeb.transform.eulerAngles.z;
        if (degree >= 270) { degree -= 270; } else { degree += 90; }
        degree *= Mathf.PI / 180;

        float x = Mathf.Cos(degree);
        float y = Mathf.Sin(degree);

        GameObject inst = (GameObject)Instantiate(Datas.Singleton.Instance.playerRole.BulletPrefeb, new Vector3(
            Datas.Singleton.Instance.playerRole.PlayerPrefeb.transform.position.x + x * Datas.Singleton.Instance.playerRole.offset,
            Datas.Singleton.Instance.playerRole.PlayerPrefeb.transform.position.y + y * Datas.Singleton.Instance.playerRole.offset,
            Datas.Singleton.Instance.playerRole.BulletPrefeb.transform.position.z
            ), Quaternion.identity);

        inst.GetComponentInChildren<Bullet>().Init(new Vector3(x, y, 0));
        inst.transform.parent = Datas.Singleton.Instance.playerRole.Bullets.transform;

    }

    void OnEnable()
    {
        EasyJoystick.On_JoystickMove += On_JoystickMove;
        EasyJoystick.On_JoystickMoveEnd += On_JoystickMoveEnd;
    }

    void OnDisable()
    {
        EasyJoystick.On_JoystickMove -= On_JoystickMove;
        EasyJoystick.On_JoystickMoveEnd -= On_JoystickMoveEnd;
    }

    void OnDestroy()
    {
        EasyJoystick.On_JoystickMove -= On_JoystickMove;
        EasyJoystick.On_JoystickMoveEnd -= On_JoystickMoveEnd;
    }


    void On_JoystickMoveEnd(MovingJoystick move)
    {
        if (move.joystickName == "PlayerController")
        {
            
        }
    }

    void On_JoystickMove(MovingJoystick move)
    {
        if (move.joystickName == "PlayerController")
        {
            // Rotate
            float degree = Mathf.Atan2(move.joystickAxis.y, move.joystickAxis.x) * 180 / Mathf.PI;
            if (degree > 90) { degree -= 90; } else { degree = degree + 270; }
            Datas.Singleton.Instance.playerRole.PlayerPrefeb.transform.eulerAngles = new Vector3(0, 0, degree);
            
            // Move
            this.PlayerCamera.transform.position = new Vector3(
                this.PlayerCamera.transform.position.x + move.joystickAxis.x / Datas.Singleton.Instance.playerRole.playerData.speedScrambler,
                this.PlayerCamera.transform.position.y + move.joystickAxis.y / Datas.Singleton.Instance.playerRole.playerData.speedScrambler,
                -10
                );
            /*
            if (Mathf.Abs(move.joystickAxis.y) > 0 && Mathf.Abs(move.joystickAxis.y) < 0.5)
            {

            }
            else if (Mathf.Abs(move.joystickAxis.y) >= 0.5)
            {

            }
            */
        }
    }
}
